package com.zhaoyd.uaademo.service;

import com.zhaoyd.uaademo.core.orm.BaseMapper;
import com.zhaoyd.uaademo.core.orm.BaseService;
import com.zhaoyd.uaademo.entity.SysUser;
import com.zhaoyd.uaademo.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhaoyd
 * @date 2020-10-28
 */
@Service
public class SysUserService extends BaseService<SysUser> {
    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    protected BaseMapper<SysUser> dao() {
        return sysUserMapper;
    }

    /**
     * 根据username查找用户
     * @param username
     * @return
     */
    public SysUser findByUsername(String username){
        if(StringUtils.isBlank(username)){
            return null;
        }
        SysUser queryBean = new SysUser();
        queryBean.setUsername(username);
        List<SysUser> list = super.list(queryBean);
        return list.size()>=1 ? list.get(0):null;
    }
    /**
     * 根据phone查找用户
     * @param phone
     * @return
     */
    public SysUser findByPhone(String phone){
        if(StringUtils.isBlank(phone)){
            return null;
        }
        SysUser queryBean = new SysUser();
        queryBean.setPhone(phone);
        List<SysUser> list = super.list(queryBean);
        return list.size()>=1 ? list.get(0):null;
    }

    /**
     * 新增一个用户
     * @param username
     * @param password
     * @param nickname
     * @return
     */
    public Integer addUser(String username, String password, String nickname){
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(password);
        sysUser.setNickname(nickname);
        sysUser.setInUse(true);
        super.save(sysUser);
        return sysUser.getId();
    }
}
